package org.ahuanlien.crm.web.controller;

import org.ahuanlien.crm.domain.Car;
import org.ahuanlien.crm.query.CarQueryObject;
import org.ahuanlien.crm.query.QueryObject;
import org.ahuanlien.crm.service.ICarService;
import org.ahuanlien.crm.util.JSONResult;
import org.ahuanlien.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("car")
public class CarController {

	@Autowired
	private ICarService carService;

	@RequestMapping("selectAll")
	@ResponseBody
	public List<Car> selectAll(QueryObject qo) {
		return carService.selectAll();
	}

	//打开页面和数据获取分开
	@RequestMapping("view")
	public String view() {
		return "car/view";
	}

	@RequestMapping("query")
	@ResponseBody
	public PageResult query(CarQueryObject qo) {
		PageResult result = carService.query(qo);
		return result;
	}

	@RequestMapping("getCar")
	@ResponseBody
	public Car getCar(Long id) {
		Car car = carService.selectByPrimaryKey(id);
		return car;
	}

	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Car car) {
		try {
			if (car.getId() != null) {
				carService.updateByPrimaryKey(car);
			} else {
				carService.insert(car);
			}
			return new JSONResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONResult().mark("保存失败");
		}
	}

	@RequestMapping("delete")
	@ResponseBody
	public JSONResult delete(Long id) {
		try {
			carService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

	/*@RequestMapping("changeState")
	@ResponseBody
	public JSONResult changeState(Long id){
		if(id != null){
			carService.changeState(id);
			return new JSONResult();
		}else {
			return new JSONResult().mark("操作失败！");
		}
	}*/
}
